// -----------------------------------------------------------------------
//  <copyright file="GetRoleByNameQuery.cs" company="LiuliuSoft">
//      Copyright (c) 2022-2024 DaprPlus. All rights reserved.
//  </copyright>
//  <site>https://dapr.plus</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2024/12/7 20:51:11</last-date>
// -----------------------------------------------------------------------

using DaprPlus.Identity.Domain.Entities;


namespace DaprPlus.Identity.Application.Identity.Roles.Queries;

[AuthConfig(typeof(Role), ResourceAccessType.RoleLimit, "GetByName")]

public class GetRoleByNameQuery : SingleQuery<RoleOutDto?>
{
    public GetRoleByNameQuery(string name)
    {
        Check.NotNullOrEmpty(name, nameof(name));
        Name = name;
    }

    public string Name { get; set; }
}

public class GetRoleByNameQueryHandler(IQueryRepository<Role> queryRepository)
    : IRequestHandler<GetRoleByNameQuery, RoleOutDto?>
{
    public async Task<RoleOutDto?> Handle(GetRoleByNameQuery request, CancellationToken cancellationToken)
    {
        var spec = new RoleByNameSpec<Role>(request.Name);
        var model = await queryRepository.GetAsync(spec, cancellationToken);
        return model?.MapTo<RoleOutDto>();
    }
}

